package com.lanswon.qzsmk.service.appManageService.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lanswon.qzsmk.annotation.SystemServiceLog;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.dao.AppClassificationMapper;
import com.lanswon.qzsmk.dao.AppKindMapper;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.AppClassification;
import com.lanswon.qzsmk.model.AppKind;
import com.lanswon.qzsmk.service.appManageService.ApplicationTypeService;
import com.lanswon.qzsmk.util.CopyBean;
import com.lanswon.qzsmk.vo.ApplicationDefVo;
import com.lanswon.qzsmk.vo.ApplicationTypeVo;
import com.lanswon.qzsmk.vo.ResponseCardFileVo;
import com.lanswon.qzsmk.vo.ResultStatus;
import com.lanswon.qzsmk.vo.ResultVo;

import ch.qos.logback.classic.Logger;



/**
 * @author dy
 *
 */
@Service
public class ApplicationTypeServiceImpl   implements   ApplicationTypeService{

//应用分类定义表映射
	@Autowired
	AppClassificationMapper  accm;
	
	@Autowired
	AppKindMapper  akm;
	@Autowired
	AppClassificationMapper   acm;
	

	private final static Logger logger = (Logger) LoggerFactory.getLogger(ApplicationTypeServiceImpl.class);
	
	@Override
	public DwzPage<ApplicationTypeVo> queryAppOpenRegByCodeKindname (ApplicationTypeVo  ato,
			String appKindName)
	{
		//System.out.println(ak.toString()+"这个是我需要的东西");
		//哒哒阿达，通过卡类型名称来查询 你懂的
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("操作成功");
		rv.setResultStatus(rs);
		
		
		List<AppClassification> listl= accm.queryAppClassificationByAppKindName(appKindName);
	
		List<ApplicationTypeVo>lists=new ArrayList<>();
        for (AppClassification appClassification : listl)
		{
        	
        	
        	//通过卡应用id来查询你懂得，呵呵哒 想了半天
        	AppKind   ak1= akm.queryAppKindByTypeCode(appClassification.getAppKindId());
        	 ApplicationTypeVo   ato1=new ApplicationTypeVo();
        	 ato1=(ApplicationTypeVo) CopyBean.copyA2b(appClassification, ato1);
        	 //捆绑一波
        	 ato1.setAccountFlag("否");
        	 ato1.setIsOnline("是");
        	 ato1.setIsAnnual("是");
        	 ato1.setIsWriteCard("是");
        	 ato1.setAllowDisable("是");
        	ato1.setValidMonthNum("3个月");
        	 //ato1.setValidMonthNum(ak. ());
        	 //添加到lists中
        	 lists.add(ato1);
		}
	
         int totalCount =  akm.countByExample(appKindName);
 		DwzPage<ApplicationTypeVo> pd = new DwzPage<ApplicationTypeVo>(ato.getCurrentPage(),
 				ato.getNumPerPage(), totalCount);
 		pd.setList(lists);
		return pd;

}
	@Override
	public int Insert(AppKind ak ,ApplicationTypeVo  ato)
	
	{
		
		return akm.insert(ak);
	}
	@Override
	public AppClassification queryApplicationTypeVoByKindname(String appKindName) {
		return    accm.queryAppClassificationByAppKindName1(appKindName);
	}
	@Override
	public List<AppClassification> queryAppClassification() {
		return acm.queryAppClassification();
	}
	@Override
	public AppClassification updateApplicationTypeVo(AppClassification cpv) {
		return akm.updateApplicationTypeVo(cpv);
	}
	@Override
	public List<AppKind> queryAppIdByKindname() {
		return  akm.queryAppKind();
	}
	@Override 
	public AppKind queryAppIdById(Integer appKindId) {
		
		return akm.queryAppKindByAppKindID(appKindId);
	}
	
	@Override
	@SystemServiceLog(description = "查询应用定义")
	public DwzPage<ApplicationDefVo> queryAppKindReg(HttpServletRequest request) {
		List<ApplicationDefVo> appTypeVoList = new ArrayList<ApplicationDefVo>();
		String pageNumString = (String)request.getParameter("pageNum");
		String numPerPageString = (String)request.getParameter("numPerPage");
		logger.debug("pageNum==>"+pageNumString);
		logger.debug("numPerPage==>"+numPerPageString);
		Integer pageNum = 1;
		Integer numPerPage = 10;
		if(!"".equals(pageNumString)&& pageNumString!=null){
			pageNum = Integer.parseInt(pageNumString);
		}
		if(!"".equals(numPerPageString)&& numPerPageString!=null){
			numPerPage = Integer.parseInt(numPerPageString);
		}
		logger.debug("pageNum==>"+pageNum);
		logger.debug("numPerPage==>"+numPerPage);
		String appName = request.getParameter("appKindNameByDef");
		String appKindId = request.getParameter("appKindId");
		logger.debug("appName==>"+appName);
		Integer startLine = (pageNum - 1)*numPerPage;
		Integer endLine = numPerPage;
		Integer totalCount =  akm.countAllDefInfo();
		appTypeVoList = akm.queryAppDefInfoByKindId(appKindId,appName,startLine,endLine);
		
		
		DwzPage<ApplicationDefVo> dp = new DwzPage<ApplicationDefVo>(pageNum, numPerPage ,totalCount);
		dp.setList(appTypeVoList);
		return dp;
	}
	@Override
	public AppKind queryAppKindInfo(Integer appType) {
		AppKind appkindObj = akm.queryAppKindByTypeCode(appType);
		return appkindObj;
	}
	
	@Override
	@SystemServiceLog(description = "记录新增应用信息")
	@Transactional(rollbackFor=Exception.class)
	public Map<String,Object> addAppDef(AppKind appKind) {
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		logger.debug("appKind==>"+appKind);
		String appName = appKind.getAppName();
		String appFlag = appKind.getAppFlag();
		Integer countObj = akm.queryAppKindInfoByName(appName,appFlag);
		if(countObj>0){
			resultMap.put("flag",false);
		}else{
			akm.insert(appKind);
			resultMap.put("flag",true);
		}
		return resultMap;
	}
	
	/*@Override
	@SystemServiceLog(description = "根据名称查询信息")
	public Map<String, Object> queryAppKindInfoByAppName(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		String appName = request.getParameter("appName");
		Integer countObj = akm.queryAppKindInfoByName(appName);
		map.put("checkName", countObj);
		return map;
	}*/
	
	@Override
	@SystemServiceLog(description = "更新应用定义")
	public void updateAppDef(AppKind appKind) {
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		appKind.setLastOperator(String.valueOf(activeUser.getUserNo()));
		akm.updateAppKindInfo(appKind);
	}


}

